package com.yjhb.android.sbz.mvp.home;

import com.yjhb.android.sbz.entity.AdBanner;
import com.yjhb.android.sbz.entity.RedPackDetails;
import com.yjhb.android.sbz.entity.ResultSet;
import com.yjhb.android.sbz.entity.SystemAnnouncement;
import com.yjhb.android.sbz.entity.TaskInfo;
import com.yjhb.android.sbz.http.HttpObserver;
import com.yjhb.android.sbz.http.HttpTool;
import com.yjhb.android.sbz.mvp.ConventionPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建者 zx
 * 创建时间 2020/12/16 14:58
 */
public class HomePresenter extends ConventionPresenter<HomeView> {
    private final String TAG = "HomePresenter";

    @Override
    protected void onDestroy() {

    }

    public HomePresenter(HomeView homeView) {
        super(homeView);
    }





    public void getHomeTops() {
        if (view != null) {
            view.onWait("正在加载..");
        }
        HttpTool.request(model.getHomeTops(null), new HttpObserver<List<TaskInfo>>() {
            @Override
            public void onResult(List<TaskInfo> data) {
                if (view != null) {
                    view.onTops(data);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }

    /**
     * 系统公告
     */
    public void getSystemNotice() {
        HttpTool.request(model.getSystemNotice(5, 1), new HttpObserver<ResultSet<SystemAnnouncement>>() {
            @Override
            public void onResult(ResultSet<SystemAnnouncement> data) {
                if (view != null) {
                    List<SystemAnnouncement> list = new ArrayList<>();
                    for (SystemAnnouncement s : data.getRecords()) {
                        if (s.getState() == 1) {
                            list.add(s);
                        }
                    }
                    view.onSysNotice(list);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }

    /**
     * 查询红包详情
     *
     * @param taskId
     */
    public void queryRedPacket(Integer taskId) {
        if (view != null) {
            view.onWait("请稍候...");
        }
        HttpTool.request(model.queryRedPacket(taskId), new HttpObserver<RedPackDetails>() {
            @Override
            public void onResult(RedPackDetails data) {
                if (view != null) {
                    view.onRedPackDetail(data);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }


    /**
     * 广告
     */
    public void getBanner() {
        HttpTool.request(model.getBanner(0,10, 1), new HttpObserver<ResultSet<AdBanner>>() {
            @Override
            public void onResult(ResultSet<AdBanner> data) {
                if (view != null) {
                    view.onBanner(data.getRecords());
                }
            }

            @Override
            public void onError(int code, String msg) {

            }
        });
    }

}
